#include <vector>
#include <string>
using namespace std;
class Solution
{
public:
    int longestPalindromeSubseq(string s)
    {
        int n = s.size(), ans = 0;
        vector<vector<int>> dp(n, vector<int>(n, 1));
        for (int i = 1; i < n; i++)
        {
            for (int j = i - 1; j >= 0; j--)
            {
                if (s[j] == s[i])
                {
                    dp[j][i] = j < i - 1 ? dp[j + 1][i - 1] + 2 : 2;
                }
                else
                    dp[j][i] = max(dp[j + 1][i], dp[j][i - 1]);
            }
        }
        return dp[0][n - 1];
    }
};